package com.jy.oms.framework.asyn.task.alarm;

import com.jy.fire.FireRobot;
import com.jy.fire.enumtype.MessageType;
import com.jy.fire.request.FireRobotMessageRequest;
import com.jy.oms.framework.asyn.task.core.ExecutorContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liuquan
 */
public class FireRobotAlarmMessageService implements ITaskAlarmMessageService {
    private static Logger logger = LoggerFactory.getLogger(FireRobotAlarmMessageService.class);
    private String fireRobotKey;
    private String fireRobotGroupTag;

    public FireRobotAlarmMessageService(String fireRobotKey, String fireRobotGroupTag) {
        this.fireRobotKey = fireRobotKey;
        this.fireRobotGroupTag = fireRobotGroupTag;
    }

    @Override
    public void alarm(AlarmMessage alarmMessage) {
        if (ExecutorContext.isDev()) {
            logger.error(alarmMessage.toString());
        } else {
            logger.error(alarmMessage.toString());

            if (StringUtils.isAnyBlank(fireRobotKey, fireRobotGroupTag)) {
                return;
            }

            try {
                FireRobot fireRobot = new FireRobot(fireRobotKey);
                FireRobotMessageRequest messageRequest = new FireRobotMessageRequest();
                messageRequest.setType(MessageType.markdown.name());
                messageRequest.setText(alarmMessage.toString());
                messageRequest.setGroupTag(fireRobotGroupTag);
                fireRobot.send(messageRequest);
            } catch (Exception e) {
                logger.error("发送消息失败", e);
            }
        }
    }
}
